package io.hujian.controller;

import com.alibaba.fastjson.JSON;
import io.hujian.graphql.GraphqlFacade;
import io.hujian.view.CheckView;
import io.hujian.view.MockerDataView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by hujian06 on 2017/11/2.
 * <p>
 * the graphql controller
 */
@Controller
@RequestMapping(value = "dsql/api/")
public class GraphqlController {
    private static final Logger logger = LoggerFactory.getLogger(GraphqlController.class);

    /**
     * check the server.
     *
     * @param request  the request
     * @param response the response
     */
    @RequestMapping(value = "check")
    public void checkAlive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        new CheckView(200, "ok").writeView(response.getOutputStream());
    }

    /**
     * text the mocker
     *
     * @param request  r
     * @param response r
     */
    @RequestMapping(value = "mocker")
    public void testMocker(HttpServletRequest request, HttpServletResponse response) throws IOException {
        new MockerDataView().writeView(response.getOutputStream());
    }

    /**
     * query the hsql by the graphql
     *
     * @param ghql     the query string like:->
     *                 "{
     *                 author(authorId:2)
     *                 {
     *                 authorId,
     *                 authorAge,
     *                 authorAddr,
     *                 friends
     *                 }
     *                 }"
     *                 the response like:->
     *                 "{
     *                 "author": {
     *                 "authorId": 2,
     *                 "authorAge": 32,
     *                 "authorAddr": "Ty-0021",
     *                 "friends": [1]
     *                 }
     *                 }"
     * @param request  r
     * @param response r
     * @throws IOException e
     */
    @RequestMapping(value = "query/{ghql}")
    public void graphqlQuery(@PathVariable("ghql") String ghql, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map map = GraphqlFacade.query(ghql);
        String result = JSON.toJSONString(map);

        System.out.println("request query:" + ghql + " \nresult:" + result);
        System.out.println(map);

        //query the result.
        response.getOutputStream().write(result.getBytes());
    }

    @RequestMapping(value = "query1")
    @ResponseBody
    public Object graphqlQuery1(@RequestBody String ghql) {
        Map map = GraphqlFacade.query(ghql);
        String result = JSON.toJSONString(map);

        System.out.println("request query:" + ghql + " \nresult:" + result);
        System.out.println(map);
        return JSON.parseObject(result);
    }

    @RequestMapping(value = "query11")
    @ResponseBody
    public Object graphqlQuery11(@RequestBody String ghql) {
        logger.info("程序执行-请求参数：" + ghql);
        return "success" + ghql;
    }

}
